import message from "./message.js";
import common from "./common.js";
import store from './store.js';

let isCloud = false;
// let baseURL = "https://yinyangshipu.vip:7777/tice/wx";

// let baseURL = "http://localhost:7710/wx/app";
// let baseURL = "https://46k67j4459.goho.co/wx/app";

let baseURL = "";

// baseURL = "https://46k67j4459.goho.co/wx";
// baseURL = "http://82.156.176.7:8010/wx";
baseURL = "http://localhost:8010/wx";
// baseURL = "https://www.easy-yoga.cn:8010/wx";

const httpObject = {
	login: function(code, fromid) {
		return httpObject.get({
			url: "/login/md/jscode?code="+code+"&fromid="+fromid,
		}).then(data => {
			store.commit("loginSuccess", data);
			uni.hideLoading();
		});
	},
	_changeParam: param => {
		param.url = baseURL+param.url;
		return param;
	},
	put: param => {
		param.method = "PUT";
		return httpObject._request(httpObject._changeParam(param));
	},
	get: param => {
		param.method = "GET";
		return httpObject._request(httpObject._changeParam(param));
	},
	post: param => {
		param.method = "POST";
		return httpObject._request(httpObject._changeParam(param));
	},
	"delete": param => {
		param.method = "DELETE";
		return httpObject._request(httpObject._changeParam(param));
	},

	_request: param => {
		// uni.showLoading({
		// 	title: '加载中',
		// 	mask: true
		// });
		return new Promise((resolve, reject) => {
			if (isCloud) {} else {
				let localHeader = {
					// 'content-type': 'application/x-www-form-urlencoded',
				};
				
				let TOKEN = common.getAttribute("TOKEN");
				
				if (TOKEN) {
					localHeader.TOKEN = TOKEN;
				}
				
				let requestOption = {
				    url: param.url,
					method: param.method,
				    data: param.data,
					withCredentials: true,
					header: localHeader,
					fail: function(f) {
						console.log(f);
					},
					error: function(e) {
						console.log(e);
					},
				    success: function(res) {
						
						if (res.header.TOKEN) {
							common.setAttribute("TOKEN", res.header.TOKEN);
						}
						let response = res.data;
				        if (response.STATUS === "SUCCESS") {
				        	resolve(response.DATA);
				        } else if (response.STATUS === "FAIL") {
							if (response.CODE) {
								uni.showModal({
								    title: '提示',
								    content: response.MESSAGE,
									showCancel: false,
								    success: function (res) {
								        if (res.confirm) {
											reject(response.CODE);
								        }
								    }
								});
							} else {
								message.warning(response.MESSAGE);
							}
				        } else if (response.STATUS === "NOT_LOGIN") {
							uni.showModal({
							    title: '登录过期',
							    content: '登录过期，重新发起登录。',
								showCancel: false,
							    success: function (res) {
							        store.commit("setRequestOption", requestOption);
							        
							        uni.login({
							        	success: function(res) {
							        		httpObject.login(res.code);
							        	}
							        });
							    }
							});
				        } else if (response.STATUS === "BINDING_FAIL") {
							let openId = response.MESSAGE;
							common.setAttribute("openId", openId);
							reject("BINDING_FAIL");
				        } else if (response.STATUS === "ERROR") {
							console.log("hahahaha");
				        	message.error(response.MESSAGE);
							reject("ERROR");
				        } else {
				        	message.error("系统出现未知错误！");
				        }
				    }
				};
				
				uni.request(requestOption);
			}
			// uni.hideLoading();
		});
	}
}

export default httpObject;